package com.yanqu.road.server.cmd.mall;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.UserActivityConditionData;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.ActivityProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.command.CmdApi;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.ActivityModule;
import com.yanqu.road.server.logic.activity.BaseActivityData;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;

import java.math.BigInteger;
import java.util.List;

@CmdApi(reqMsg = ActivityProto.SystemOpenSystemOpenRechargeNoticeReqMsg.class, respMsg = ActivityProto.SystemOpenSystemOpenRechargeNoticeRespMsg.class)
@Cmd(code = Protocol.S_SYSTEM_OPEN_RECHARGE_NOTIFY, desc = "系统开放触发礼包")
public class SystemOpenRechargeNotifyCmd extends AbstractCommand {

    private int activityId;
    private int conditionType;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        ActivityProto.SystemOpenSystemOpenRechargeNoticeReqMsg reqMsg = ActivityProto.SystemOpenSystemOpenRechargeNoticeReqMsg.parseFrom(packet.getBytes());
        activityId = reqMsg.getActivityId();
        conditionType = reqMsg.getConditionType();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        ActivityInfo activityInfo = ActivityMgr.getOpenActivityInfo(activityId);
        if (activityInfo != null && activityInfo.getType() == eActivityType.SystemOpenRMBMallActivity.getValue()) {
            ActivityModule activityModule = player.getModule(ActivityModule.class);
            BaseActivityData baseActivityData = activityModule.getBaseActivityData(activityId);
            if (null == baseActivityData) {
                ret = GameErrorCode.E_ACTIVITY_NO_FOUND;
                return;
            }
            boolean notice = false;
            List<UserActivityConditionData> cList = baseActivityData.getUserActivityConditionDataList();
            for (UserActivityConditionData conditionData : cList) {
                if (conditionData.getType() == conditionType && conditionData.getValue().equals(BigInteger.ZERO)) {
                    notice = true;
                }
            }
            if (notice) {
                player.notifyListener(conditionType, -666);
            }
        } else {
            ret = GameErrorCode.E_ERROR_PARAMETER;
            return;
        }
    }

    @Override
    public void sendData(GamePlayer player) {
        ActivityProto.SystemOpenSystemOpenRechargeNoticeRespMsg.Builder respMsg = ActivityProto.SystemOpenSystemOpenRechargeNoticeRespMsg.newBuilder();
        respMsg.setRet(ret);
        player.sendPacket(ClientProtocol.U_SYSTEM_OPEN_RECHARGE_NOTIFY, respMsg);
    }
}
